package com.da.sumup.service.impl;

import com.da.sumup.dao.bean.ArticleDO;
import com.da.sumup.dao.repsository.ArticleRepository;
import com.da.sumup.pojo.ArticleBO;
import com.da.sumup.pojo.PageBO;
import com.da.sumup.service.IArticleService;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl implements IArticleService {

    private final ArticleRepository articleRepository;

    private final ModelMapper modelMapper;

    public ArticleServiceImpl(ArticleRepository articleRepository, ModelMapper modelMapper) {
        this.articleRepository = articleRepository;
        this.modelMapper = modelMapper;
    }

    @Override
    public Page<ArticleDO> findAll(PageBO pageBO, List<String> sorts) {
        List<Sort.Order> orders = sorts.stream().map(sort -> new Sort.Order(Sort.Direction.DESC, sort)).collect(Collectors.toList());
        Sort sort = Sort.by(orders);
        Pageable pageable = PageRequest.of(pageBO.getPage()-1, pageBO.getLimit(),sort);

        ArticleDO whereBean = new ArticleDO();
        String type = pageBO.getQueryParam().get("type");
        if(StringUtils.hasText(type))
            whereBean.setCatalogueUuid(type);
        String title = pageBO.getQueryParam().get("title");
        if(StringUtils.hasText(title))
            whereBean.setTitle(title);
        ExampleMatcher matcher = ExampleMatcher.matching().
                withMatcher("title",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("version");
        Example<ArticleDO> example = Example.of(whereBean,matcher);
        return articleRepository.findAll(example,pageable);
    }

    @Override
    public void save(ArticleBO articleBO) {
        ArticleDO articleDO = modelMapper.map(articleBO, ArticleDO.class);
        String catalogue = articleBO.getCatalogueUuid();
        String[] catalogueStr = catalogue.split("_");
        articleDO.setCatalogueUuid(catalogueStr[0]);
        articleDO.setCatalogueName(catalogueStr[1]);
        articleRepository.save(articleDO);
    }

    @Override
    public ArticleDO findOne(String uuid) {
        return articleRepository.findById(uuid).get();
    }

    @Override
    public List<ArticleDO> findAll() {
        return articleRepository.findAll();
    }

    @Override
    public List<ArticleDO> findMain(String title) {
        ArticleDO whereBean = new ArticleDO();
        if(StringUtils.hasText(title))
            whereBean.setTitle(title);
        else
            whereBean.setCover("hot");
        ExampleMatcher matcher = ExampleMatcher.matching().
                withMatcher("title",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("version");
        Example<ArticleDO> example = Example.of(whereBean,matcher);
        return articleRepository.findAll(example);
    }

    @Override
    public void delete(String uuid) {
        articleRepository.deleteById(uuid);
    }
}
